<template>
	<view>
		<view style="width: 100%;margin: 0 auto;">
			<u-tabs :list="classList" :is-scroll="true" :current="current" @change="change"></u-tabs>
		</view>
		<al-goods-list :data="data" clickUrl="/pages/detail/index"></al-goods-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				page: 0,
				length: 10,
				data: [],
				status: 'loadmore',
				dataLength: 0,
				classList: [],
				current: 0,
				id: ''
			}
		},
		onLoad(e) {
			this.id = e.id
		},
		onReachBottom() {
			// console.log('1111')
			if (this.dataLength < 10) return;
			this.status = 'loading';
			this.page = this.length;
			this.length = this.length + 10
			setTimeout(() => {
				this.getCouponList()
				if (this.length < 10) this.status = 'nomore';
				else this.status = 'loading';
			}, 2000)
		},
		onShow() {
			this.getCouponList()
			this.getClass()
		},
		methods: {
			change(index) {
				this.data =[]
				this.current = index;
				this.id = this.classList[index].id
				this.getCouponList()
			},
			getClass () {
				this.$request('', '/api/app/coupon/coupontype', 'POST', {
				}, {}).then(res => {
					for (let i in res.data) {
						this.classList.push({name: res.data[i].name, id: res.data[i].id})
						if (res.data[i].id == this.id) {
							console.log(i)
							this.current = i
						}
					}
				})
			},
			getCouponList() {
				this.length = 99
				this.page = 0
				this.$request('', '/api/app/coupon/goodslist', 'POST', {
					length: this.length,
					start: this.page,
					goods_name: this.keyword,
					subtitle_title: this.keyword,
					goods_type_id: this.id,
					city: uni.getStorageSync('city') ? uni.getStorageSync('city') : '长沙市'
				}, {}).then(res => {
					for (let i in res.data) {
						this.data.push(res.data[i])
					}
					this.dataLength = res.data.length
					if (this.dataLength < 10) {
						this.status = 'nomore'
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}
</style>
